package com.lute.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

import com.lute.json.ClientNameJSON;
import com.lute.utils.DAOFactory;

@Entity
public class ClientAdminProfile {
	private int id_clientadmin_profile;
	private ClientAdmin clientAdmin;
	private String email;
	private Integer defaultClientId; 
	private List<Client> clients = new ArrayList<Client>();
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId_clientadmin_profile() {
		return id_clientadmin_profile;
	}
	public void setId_clientadmin_profile(int id_clientadmin_profile) {
		this.id_clientadmin_profile = id_clientadmin_profile;
	}
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id_clientadmin")
	public ClientAdmin getClientAdmin() {
		return clientAdmin;
	}
	public void setClientAdmin(ClientAdmin clientAdmin) {
		this.clientAdmin = clientAdmin;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Integer getDefaultClientId() {
		return defaultClientId;
	}
	public void setDefaultClientId(Integer defaultClientId) {
		this.defaultClientId = defaultClientId;
	}
	
	@ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.REMOVE},fetch=FetchType.LAZY)
	@JoinTable(name="ClientAdminProfile_To_Client")
	public List<Client> getClients() {
		return clients;
	}

	public void setClients(List<Client> clients) {
		this.clients = clients;
	}
	
	public static ArrayList<ClientNameJSON> getClientsIdAndName(int propertyValue) {
		ArrayList<ClientNameJSON> clientNameJSON = new ArrayList<ClientNameJSON>();
		ClientAdminProfile clientAdminProfile = getClientAdminProfileByClientAdminIdWithClientsFromDB(propertyValue);
		List<Client> clients = clientAdminProfile.getClients();
		for(Client client : clients) {
			ClientNameJSON clientName = new ClientNameJSON();
			clientName.setClientId(Integer.toString(client.getId_client()));
			clientName.setClientName(client.getName());
			clientNameJSON.add(clientName);
		}
		return clientNameJSON;
	}
	
	public static ClientAdminProfile getClientAdminProfileByClientAdminIdWithClientsFromDB(int propertyValue) {
		ClientAdminProfile clientAdminProfile = 
	            DAOFactory.getInstance().getLuteInterface().
	            		getClientAdminProfileByClientAdminIdWithClientsFromDataBase(propertyValue);
		return clientAdminProfile;
	}
	
	public static ClientAdminProfile getClientAdminProfileByClientAdminIdFromDB(int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientAdminProfileByClientAdminIdFromDataBase(propertyValue);
	}
	
	public static List<ClientAdminProfile> getClientAdminProfilesFromDB() {
		return DAOFactory.getInstance().getLuteInterface().getClientAdminProfilesFromDataBase();
	}
	
	public static List<ClientAdminProfile> getClientAdminProfilesByClientIdFromDB(int clientId) {
		List<ClientAdminProfile> profiles = DAOFactory.getInstance().getLuteInterface().getClientAdminProfilesWithClientsFromDataBase();
		List<ClientAdminProfile> clientAdminProfiles = new ArrayList<ClientAdminProfile>();
		for(ClientAdminProfile profile: profiles) {
			List<Client> clients = profile.getClients();
			for(Client client : clients) {
				if(client.getId_client() == clientId) {
					clientAdminProfiles.add(profile);
				}
			}
		}
		return clientAdminProfiles;
	}
	
	public static List<ClientAdminProfile> getClientAdminProfilesByUserAccLockedFromDB(String propertyName, String propertyValue) {
		List<ClientAdmin> clientAdmins = ClientAdmin.getClientAdminByAccLockedFromDB(propertyName, propertyValue);
		ArrayList<ClientAdminProfile> profiles = new ArrayList<ClientAdminProfile>(); 
		for(ClientAdmin clientAdmin: clientAdmins) {
			profiles.add(clientAdmin.getClientAdminProfil());
		}
		return profiles;
	}
	
	public static void updateClientAdminProfileInDB(ClientAdminProfile profile) {
		DAOFactory.getInstance().getLuteInterface().updateObjectinDataBase(profile);
	}
	
	public static void addClientAdminProfileToDB(ClientAdminProfile profile) {
		DAOFactory.getInstance().getLuteInterface().addObjectToDataBase(profile);
	}
}
